package com.zrrd.mapperTest;

import com.zrrd.common.utils.SqlSessionFactoryUtil;
import com.zrrd.entity.Tea;
import com.zrrd.mapper.TeaMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author WanJl
 * @version 1.0
 * @title TeaMapperTest02
 * @description
 * @create 2024/10/11
 */
public class TeaMapperTest02 {
    @Test
    public void queryAll(){
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
        List<Tea> teaList = teaMapper.selectAll();
        for (Tea tea : teaList) {
            System.out.println(tea);
        }
    }

    public void queryAlll() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession(true);//自动提交事务

        SqlSessionFactoryUtil.getSqlSession(true);

        TeaMapper mapper = sqlSession.getMapper(TeaMapper.class);
        mapper.add(new Tea());
        //手动提交事务
        //sqlSession.commit();

    }

    @Test
    public void queryByCondition(){
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        TeaMapper mapper = sqlSession.getMapper(TeaMapper.class);
        Tea tea = new Tea();
        //tea.setTeaName("金骏眉");
        tea.setTeaTypeId(1);
        List<Tea> teaList = mapper.selectCondition(tea);

        //遍历并输出到控制台
        teaList.forEach(System.out::println);

    }
    @Test
    public void selectById(){
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        TeaMapper mapper = sqlSession.getMapper(TeaMapper.class);
        Tea tea = mapper.selectById(1);
        System.out.println(tea);
    }

    @Test
    public void selectByIds(){
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        TeaMapper mapper = sqlSession.getMapper(TeaMapper.class);
        List<Integer> list=new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);

        List<Tea> teaList = mapper.selectByIds(list);

        /*for (Tea tea : teaList) {
            System.out.println(tea);
        }*/
        teaList.forEach(System.out::println);
    }
}
